<?php
include_once("model/user.class.php");

/**
 * dashboard class. This is a class used to process all
 * logic of dashboard tab in admin page.
 */
class forgotpass 
{
	var $db;
 	var $smarty;
 	
    function forgotpass($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/**
	 * This function will be call be index.php base on mod specified by GET param
	 */
	function process()
	{
		if(isset($_POST['username']) && $_POST['username'] !=''){
			$user = user::getUserByUserName($db,$_POST['username'])	;	
			if($user!=''){
				$newpass = md5(date("YmdHms").mt_rand());
				$from = $_POST['email'];
				$to = $user->user_email ;
				$subject = "Reset password Email";
				$message = 	"New pass:  " . $newpass;
				$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $from";
				user::updatePass($db,$newpass,$user->user_login,$user->ID);
				if (mail($to, $subject, $message, $header)){
					$type = array('ID' => 0,'message' => "Succed");
					
				} 
				else 
				{
					if($_SESSION["LC_ALL"] == "en")
					{
						$type = array('ID' => -1, 
									  'message' => "Error occurred while sending email. Please try again.");
					}
					else
					{
						$type = array('ID' => -1, 
									  'message' => "Lỗi trong quá trình gửi email. Vui lòng thử lại.");
					}
				}
			}
		}
		
		$this->smarty->display('forgot.tpl');
	}
}
?>